package com.seek.eduservice.controller;

import com.seek.commonutils.ResponseResult;
import com.seek.eduservice.entity.EduComment;
import com.seek.eduservice.entity.vo.CommentQuery;
import com.seek.eduservice.entity.vo.CommentVo;
import com.seek.eduservice.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-03-25
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    // 条件查询带分页的方法
    @GetMapping("pageCondition/{current}/{size}")
    public ResponseResult pageCondition(@PathVariable int current, @PathVariable int size,
                                        CommentQuery commentQuery) {
        int start = (current - 1) * size;
        long total = eduCommentService.count(commentQuery);
        List<CommentVo> records = eduCommentService.pageCondition(start, size, commentQuery);
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    // 根据评论 id 进行查询
    @GetMapping("{id}")
    public ResponseResult getById(@PathVariable String id) {
        CommentVo commentVo = eduCommentService.getCommentInfoById(id);
        return ResponseResult.ok().data("commentVo", commentVo);
    }

    // 根据评论 id 修改是否显示
    @PutMapping("{id}/{isShow}")
    public ResponseResult updateIsShowById(@PathVariable String id, @PathVariable Integer isShow) {
        EduComment eduComment = new EduComment();
        eduComment.setId(id);
        eduComment.setIsShow(isShow == 0 ? 1 : 0);
        boolean result = eduCommentService.updateById(eduComment);
        if (result) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.error();
        }
    }

}

